package com.huobao.yunying.adapters;

import java.util.List;

import com.huobao.yunying.activitys.Act_Ketang_detail;
import com.huobao.yunying.entity.KetangGridFather;
import com.huobao.yunying.views.NoScrollGridView;
import com.huobao.zhangying.R;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class Adapter_ketang_ extends BaseExpandableListAdapter{

	private LayoutInflater inflater;
	private Context context;
	private List<KetangGridFather> list;
	public Adapter_ketang_(Context context, List<KetangGridFather> list){
		this.inflater = LayoutInflater.from(context);
		this.context = context;
		this.list = list;
	}
	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return list.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return list.get(groupPosition).getCourses().get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		if(convertView == null)
			convertView = inflater.inflate(R.layout.item_ketang_groupview, null);
		TextView tv_title = (TextView) convertView.findViewById(R.id.tv_title);
		KetangGridFather father = list.get(groupPosition);
		tv_title.setText(father.getClass_name());
		return convertView;
	}

	@Override
	public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		if(convertView == null)
			convertView = inflater.inflate(R.layout.item_ketang_item, null);
		NoScrollGridView gridview = (NoScrollGridView) convertView.findViewById(R.id.gridview);
		final Adapter_ketang_GridView adapter = new Adapter_ketang_GridView(context,list.get(groupPosition).getCourses());
		gridview.setAdapter(adapter);
		gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(context, Act_Ketang_detail.class);
				intent.putExtra("more", list.get(groupPosition).getCourses().get(position));
				context.startActivity(intent);
			}
		});
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return false;
	}

}
